<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2022/8/31
 * Time: 17:34:38
 */

namespace app\common\service\CouponService;

use app\Normal;
use app\model\BbcModel\CouponStore;

class CouponStoreService
{
    /**
     * 添加优惠券适用店铺
     * @param $param
     * @return void
     */
    public function addData( array $param): array{
        if(!$param){
            return Normal::returnCode(Normal::FAIL,'缺少优惠券参数');
        }
        $time = time();
        $cid = $param['cid'];
        $coupon_id = $param['coupon_id'];
        $stores = $param['mn_sids'];
        if($param['mn_sids'] == 0){ //所有店铺
            $res = CouponStore::instance()->add(['cd'=>$time,'cid'=>$cid,'cb_id'=>$param['cb_id'],'coupon_id'=>$coupon_id,'mn_sid'=>0]);
            if($res == false){
                return Normal::returnCode(Normal::FAIL,'店铺优惠券写入失败');
            }
            return Normal::returnCode(Normal::SUC,'店铺优惠券写入成功');
        }
        $store_arr = explode(',',$stores);
        foreach($store_arr as $k=>$v){
            $insertStores[$k]['cd'] = $time;
            $insertStores[$k]['cid'] = $cid;
            $insertStores[$k]['cb_id'] = $param['cb_id'];
            $insertStores[$k]['mn_sid'] = $v;
            $insertStores[$k]['coupon_id'] = $coupon_id;
        }
        $res = CouponStore::instance()->insertAll($insertStores);
        if($res == false){
            return Normal::returnCode(Normal::FAIL,'店铺优惠券写入失败');

        }
        return Normal::returnCode(Normal::SUC,'店铺优惠券写入成功');
    }
}